package com.ymatou.iapi.csim.testcase;

import java.util.Calendar;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.GetMessageListBean;
import com.ymatou.iapi.csim.parameter.GetUserMessageListResp;
import com.ymatou.iapi.csim.parameter.OpenSessionBean;
import com.ymatou.iapi.csim.parameter.SendScoreMessageBean;
import com.ymatou.iapi.csim.parameter.SendScoreMessageWXBean;
import com.ymatou.iapi.csim.service.GetMessageListCall;
import com.ymatou.iapi.csim.service.OpenSessionCall;
import com.ymatou.iapi.csim.service.SendScoreMessageWXCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
/**
*微信用户评分
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendScoreMessageWX{
	private static SendScoreMessageWXBean bean;
	private static SendScoreMessageWXCall call;
	private CsimCallService service=new CsimCallService(); 
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private static Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("微信用户评分");
		//删除当前userId的会话，防止影响case数据(userId如果存在会话，已经评分后，不能再评分，故执行之前先删除userId的会话)
		csSessionWapper.deleteByUserId(MapUtil.hashMap("UserId",userId,"Status",0));
	}

	@Before
	public void caseUp() {
		bean=new SendScoreMessageWXBean();
		call=new SendScoreMessageWXCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessageWX_1() {
		Logger.start(true,"微信用户评分-会话结束,客服发起评分,微信用户提交评分-验证正常场景");
		try {
			//打开会话
			Logger.comment("userId"+userId);
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发送评分
			SendScoreMessageBean sendscoreMessageBean=service.setSendScoreMessageBean(csId,userId);
			sendscoreMessageBean.setCsId(csId);
			sendscoreMessageBean.setUserId(userId);
			service.SendScoreMessageCall(sendscoreMessageBean);
			
    		bean.setSessionId(cssession.getId());
    		bean.setScore(5);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("0",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals("",call.getResult().get("ErrorMessage"), "验证返回的errorMessage");
			Logger.verifyEquals(true,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals("",call.getMsg().toString(), "验证返回的Msg");
			Logger.verifyEquals("200",call.getStatus(), "验证返回的status");
			//验证数据库ymt_cssession
		    cssession=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
			Logger.verifyEquals(bean.getScore(), cssession.getScore(), "验证数据库Score与传入提交的一致");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessageWX_2() {
		Logger.start(true,"微信用户评分-会话结束,客服发起评分,微信用户提交评分-验证已经评分");
		try {
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发送评分
			SendScoreMessageBean sendscoreMessageBean=service.setSendScoreMessageBean(csId,userId);
			sendscoreMessageBean.setCsId(csId);
			sendscoreMessageBean.setUserId(userId);
			service.SendScoreMessageCall(sendscoreMessageBean);
			
    		bean.setSessionId(cssession.getId());
    		bean.setScore(5);
			call.setData(bean);
			call.callService();	
			call.callService();	
			
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("评分失败:已评分"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getMsg().toString().contains("评分失败:已评分"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
			//验证数据库ymt_cssession
		    cssession=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
			Logger.verifyEquals(bean.getScore(), cssession.getScore(), "验证数据库Score与传入提交的一致");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessageWX_3() {
		Logger.start(true,"微信用户评分-会话结束,客服发起评分,微信用户提交评分-验证会话不存在，评分失败");
		try {
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发送评分
			SendScoreMessageBean sendscoreMessageBean=service.setSendScoreMessageBean(csId,userId);
			sendscoreMessageBean.setCsId(csId);
			sendscoreMessageBean.setUserId(userId);
			service.SendScoreMessageCall(sendscoreMessageBean);
			
    		bean.setSessionId("111");
    		bean.setScore(5);
			call.setData(bean);
			call.callService();	
			call.callService();	
			
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("评分失败:不可以评分"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getMsg().toString().contains("评分失败:不可以评分"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessageWX_4() {
		Logger.start(true,"微信用户评分-会话结束,客服发起评分,微信用户提交评分-验证评分为0");
		try {
			//打开会话
			Logger.comment("userId"+userId);
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发送评分
			SendScoreMessageBean sendscoreMessageBean=service.setSendScoreMessageBean(csId,userId);
			sendscoreMessageBean.setCsId(csId);
			sendscoreMessageBean.setUserId(userId);
			service.SendScoreMessageCall(sendscoreMessageBean);
			
    		bean.setSessionId(cssession.getId());
    		bean.setScore(0);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("0",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals("",call.getResult().get("ErrorMessage"), "验证返回的errorMessage");
			Logger.verifyEquals(true,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals("",call.getMsg().toString(), "验证返回的Msg");
			Logger.verifyEquals("200",call.getStatus(), "验证返回的status");
			//验证数据库ymt_cssession
		    cssession=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
			Logger.verifyEquals(bean.getScore(), cssession.getScore(), "验证数据库Score与传入提交的一致");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessageWX_5() {
		Logger.start(true,"微信用户评分-会话结束,客服发起评分,微信用户提交评分-验证评分失效");
		try {
			//打开会话
			Logger.comment("userId"+userId);
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			
			Logger.comment("cssession.getLastMessageTime():"+cssession.getLastMessageTime());
			//修改ymt_cssession 中的LastMessageTime
			Date dNow = new Date(); // 当前时间
            Date dBefore=new Date();
			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(cssession.getLastMessageTime());// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -3); 
			dBefore = calendar1.getTime(); 
			cssession.setLastMessageTime(dBefore);
			cssession.setIsSendScore(1);
			csSessionWapper.updateByPrimaryKey(cssession);
			
    		bean.setSessionId(cssession.getId());
    		bean.setScore(5);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("评分失败:已失效"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getMsg().toString().contains("评分失败:已失效"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}